package com.dream.wuliu.view.adapter;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.vo.api.ApiOrder;
import com.dream.wuliu.view.R;

public class OrderListAdapter extends ArrayAdapter<ApiOrder> {
	private Context context;
	private List<ApiOrder> items;
	public List<ApiOrder> filtered;
	private Filter filter;

	public OrderListAdapter(Context context, List<ApiOrder> orders) {
		super(context, R.layout.order_list_data, 0, orders);
		this.context = context;
		this.filtered = orders;
		this.items = new ArrayList<ApiOrder>(orders);
	}

	@Override
	public Filter getFilter() {
		if (filter == null)
			filter = new ModelFilter();
		return filter;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ListAdapterHolder holder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(context).inflate(R.layout.order_list_data, parent, false);
			holder = new ListAdapterHolder();

			holder.tvOrderCustomer = (TextView) convertView
					.findViewById(R.id.tvOrderCustomer);
			holder.tvOrderId = (TextView) convertView
					.findViewById(R.id.tvOrderId);
			holder.tvOrderPhone = (TextView) convertView
					.findViewById(R.id.tvOrderPhone);
			//holder.tvOrderDate = (TextView) convertView .findViewById(R.id.tvOrderDate);
			holder.imgMore = (ImageView) convertView.findViewById(R.id.imgMore);
			convertView.setTag(holder);

		} else {
			holder = (ListAdapterHolder) convertView.getTag();
		}

		ApiOrder item = filtered.get(position);

		if (item != null) {

			holder.tvOrderCustomer.setText(item.getCustomer());
			holder.tvOrderId.setText(item.getCode());
			holder.tvOrderPhone.setText(item.getPhone());
			holder.imgMore.setTag(item.getId());
			//holder.tvOrderDate.setText(item.getDate());

		}

		return convertView;
	}

	class ModelFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {

			FilterResults result = new FilterResults();

			if (constraint != null && !constraint.toString().isEmpty()) {
				constraint = constraint.toString().toLowerCase();
				List<ApiOrder> resultItems = new ArrayList<ApiOrder>();
				for (ApiOrder item : items) {
					if (item.getCustomer().toLowerCase().contains(constraint) || item.getPhone().contains(constraint)) {
						resultItems.add(item);
					}
				}
				result.count = resultItems.size();
				result.values = resultItems;
			} else {
				synchronized (this) {
					result.count = items.size();
					result.values = items;
				}
			}

			return result;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			filtered = (ArrayList<ApiOrder>) results.values;
			
			notifyDataSetChanged();
			clear();
			for (ApiOrder item : filtered) {
				add(item);
			}
			notifyDataSetInvalidated();
		}

	}

	class ListAdapterHolder {

		TextView tvOrderId;
		TextView tvOrderCustomer;
		TextView tvOrderPhone;
		//TextView tvOrderDate;
		ImageView imgMore;

	}
}
